//
//  FormViewController.swift
//  iModern
//
//  Created by Ralf on 15/6/24.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit
import XLForm

class FormViewController: XLFormViewController
{
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    override func viewDidLoad()
    {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func pushViewController(controller: UIViewController!)
    {
        if self.navigationController?.respondsToSelector("interactivePopGestureRecognizer:") != nil
        {
            self.navigationController?.interactivePopGestureRecognizer!.enabled = true
            self.navigationController?.interactivePopGestureRecognizer!.delegate = nil
        }
        self.view.endEditing(true)
        self.navigationController?.pushViewController(controller, animated: true)
    }

    func login()
    {
        self.view.endEditing(true)
        let controller = LoginViewController()
        pushViewController(controller)
    }

    func initBackButton()
    {
        let image  = UIImage(named: "Back-Small.png")
        let button = UIBarButtonItem(image: image?.imageWithRenderingMode(.AlwaysOriginal), style: .Plain, target: self, action: "popViewController:")
        self.navigationItem.leftBarButtonItem = button
    }

    @IBAction func popViewController(sender: AnyObject)
    {
        self.view.endEditing(true)
        self.navigationController?.popViewControllerAnimated(true)
    }

    func alertNetworkConnectionException()
    {
        let alertView = UIAlertView(title: "提示", message: "网络连接异常，请检查你的网络设置", delegate: nil, cancelButtonTitle: "确定")
        alertView.show()
    }

    func alertUserStatusException()
    {
        let alertView = UIAlertView(title: "提示", message: "您的帐号可能已经在另一台设备上登录", delegate: nil, cancelButtonTitle: "确定")
        alertView.show()
    }

    func alertErrorMessage(message: String!)
    {
        let alertView = UIAlertView(title: "提示", message: message, delegate: nil, cancelButtonTitle: "确定")
        alertView.show()
    }

    func alertWarningMessage(message: String!)
    {
        let alertView = UIAlertView(title: "提示", message: message, delegate: nil, cancelButtonTitle: "确定")
        alertView.show()
    }

    func alertSuccessMessage(message: String!)
    {
        let alertView = UIAlertView(title: "提示", message: message, delegate: nil, cancelButtonTitle: "确定")
        alertView.show()
    }

    func verifyResultset(rs: ResultSet?) -> Bool
    {
        if rs == nil
        {
            alertNetworkConnectionException()
            return false
        }

        if rs!.hasErrors()
        {
            if rs?.errorCode == 3020
            {
                alertUserStatusException()
                NSNotificationCenter.defaultCenter().postNotificationName("Logout", object: nil)
                self.view.endEditing(true)
                navigationController?.popToRootViewControllerAnimated(true)
                return false
            }

            alertErrorMessage(rs!.errorMessage)
            return false
        }

        return true
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath) as UITableViewCell
//        cell.textLabel?.font = UIFont.systemFontOfSize(14)
        return cell
    }
}
